package cn.signalpha.modules.shop.service;

import cn.signalpha.core.tool.utils.ObjectUtil;
import cn.signalpha.modules.shop.entity.Footprint;
import cn.signalpha.modules.shop.mapper.FootprintMapper;
import cn.signalpha.modules.shop.vo.FootprintVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class FootprintService extends ServiceImpl<FootprintMapper, Footprint> {

	/**
	 * 自定义分页
	 */
    public IPage<FootprintVO> selectFootprintPage(IPage<FootprintVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectFootprintPage(page, params));
    }

	/**
	 * 保存足迹
	 */
	@Async
	public void saveFootprint(Long userId, Long goodsId) {
		Date dateTime = new Date();
		Footprint footprint = baseMapper.selectOne(Wrappers.<Footprint>lambdaQuery()
			.eq(Footprint::getUserId, userId).eq(Footprint::getGoodsId, goodsId).last("limit 1")
		);
		if(ObjectUtil.isNotEmpty(footprint)){
			footprint.setUpdateTime(dateTime);
			updateById(footprint);
		}else{
			footprint = new Footprint();
			footprint.setUserId(userId);
			footprint.setGoodsId(goodsId);
			footprint.setCreateTime(dateTime);
			footprint.setUpdateTime(dateTime);
			save(footprint);
		}
	}

}
